package com.gorkr.cronback.pattern.designPattern.proxy;

import com.gorkr.cronback.pattern.CronPattern;
import com.gorkr.cronback.pattern.designPattern.factory.ValueMatcherFactory;

import java.util.Calendar;

/**
 * @author gorkr
 * @date 2021/12/27 11:13
 **/
public class ProxyCronPattern implements CronPatternInterface {
    private CronPattern cronPattern;
    private String pattern;
    private ValueMatcherFactory valueMatcherFactory;

    public ProxyCronPattern(String pattern, ValueMatcherFactory valueMatcherFactory) {
        this.pattern = pattern;
        this.valueMatcherFactory = valueMatcherFactory;
    }

    /**
     * 仅暴露match接口， 更加安全
     * @param calendar 时间
     * @return
     * @throws Exception
     */
    @Override
    public boolean match(Calendar calendar) throws Exception {
        if(cronPattern == null){
            cronPattern = new CronPattern(pattern,valueMatcherFactory);
        }

        return  cronPattern.match(calendar);
    }
}
    